package com.reformer.commons.json;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.reformer.commons.constant.DateTimeFormat;

import java.sql.Timestamp;

/**
 * Gson工厂
 *
 * @author Administrator
 */
public class GsonFactory {

    private static GsonBuilder gsonBuilder = new GsonBuilder().setLenient();

    private GsonFactory() {
    }

    /**
     * @param format 日期格式
     * @return
     */
    public static Gson createGsonOfExposeAnnotaton(DateTimeFormat format) {
        if (format == null) {
            format = DateTimeFormat.YYYY_MM_DD_HH_MM_SS;
        }
        return gsonBuilder.excludeFieldsWithoutExposeAnnotation().setDateFormat(format.getFormat()).create();
    }

    /**
     * @param format 日期格式
     * @return
     */
    public static Gson createGson(DateTimeFormat format) {
        if (format == null) {
            format = DateTimeFormat.YYYY_MM_DD_HH_MM_SS;
        }
        return gsonBuilder.setDateFormat(format.getFormat()).create();
    }

    public static Gson createGsonOfTimestamp() {
        return gsonBuilder.registerTypeAdapter(Timestamp.class, GsonTimestampAdapter.getInstance()).create();
    }

    /**
     * @param format 日期格式
     * @return
     */
    public static Gson createGsonOfPrettyPrint(DateTimeFormat format) {
        if (format == null) {
            format = DateTimeFormat.YYYY_MM_DD_HH_MM_SS;
        }
        return gsonBuilder.setDateFormat(format.getFormat()).setPrettyPrinting().create();
    }
}
